package com.demo.plugin.run;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 创建deylop
 */
@Mojo(name = "yaml")
public class DeylopMajo extends AbstractMojo {

    /**
     * 项目路径
     */
    @Parameter(property = "basedir")
    private String  basedir;

    /**
     * 端口号
     */
    @Parameter(property = "port")
    private String port;

    /**
     * 服务名
     */
    @Parameter(property = "serverName")
    private String serverName;

    /**
     * 镜像名
     */
    @Parameter(property = "image")
    private String image;

    /**
     * host
     */
    @Parameter(property = "host")
    private String host;


    /**
     * dockerUrl镜像地址
     */
    @Parameter(property = "dockerUrl")
    private String dockerUrl;


//    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        try {
            createYaml(basedir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建yaml
     * @param baseDir
     * @throws IOException
     */
    private void createYaml(String baseDir) throws IOException {

        baseDir=baseDir+"/deylop/";
        //创建docker文件
        File yamlFile=new File(baseDir,image+".yaml");
        //创建文件路径
        createFilePath(yamlFile);
        //填写yaml
        createYamlInfo(yamlFile);
    }

    /**
     * 创建yaml信息
     * @param yamlFile 文件
     */
    private void createYamlInfo(File yamlFile)  {
        FileWriter fw =null;
        BufferedWriter bw = null;
        try {
            //对原文件进行覆盖
            fw = new FileWriter(yamlFile,false);
            bw = new BufferedWriter(fw);
            String line="\r\n";
            //创建deylopment的信息
            createDeploymentInfo(bw, line);
            //创建service信息
            createServiceInfo(bw, line);
            //创建ingress信息
            createIngressInfo(bw, line);


            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    private void createIngressInfo(BufferedWriter bw, String line) throws IOException {
        /**
         * apiVersion: extensions/v1beta1
         * kind: Ingress
         * metadata:
         *   name: by-domain-traefik-ingress
         *   annotations:
         *     kubernetes.io/ingress.class: traefik
         *     ingress.kubernetes.io/ssl-redirect: "false"
         *     traefik.frontend.rule.type: "PathPrefixStrip"
         *     traefik.ingress.kubernetes.io/frontend-entry-points: "http,https"
         * spec:
         *   rules:
         *   - host: docker.fuyi.com
         *     http:
         *       paths:
         *       - path: /
         *         backend:
         *           serviceName: docker-demo-svc
         *           servicePort: 8008
         */
        bw.write("---"+line);
        bw.write("apiVersion: extensions/v1beta1"+line);
        bw.write("kind: Ingress"+line);
        bw.write("metadata:"+line);
        bw.write("   name: by-domain-traefik-ingress"+line);

        String  dev=  System.getProperty("env");
        if(dev==null){
            bw.write("   namespace: dev"+line);
        }else{
            bw.write("   namespace: "+dev+line);
        }


        bw.write("   annotations:"+line);
        bw.write("     kubernetes.io/ingress.class: traefik"+line);
        bw.write("     ingress.kubernetes.io/ssl-redirect: \"false\""+line);
        bw.write("     traefik.ingress.kubernetes.io/frontend-entry-points: \"http,https\""+line);
        bw.write("spec:"+line);
        bw.write("   rules:"+line);
        bw.write("   - host: "+host+line);
        bw.write("     http:"+line);
        bw.write("      paths:"+line);
        bw.write("      - path: / "+line);
        bw.write("        backend:"+line);
        bw.write("           serviceName: "+serverName+line);
        bw.write("           servicePort: "+port+line);
    }

    /**
     * 创建serviceInfo
     * @param bw
     * @param line
     * @throws IOException
     */
    private void createServiceInfo(BufferedWriter bw, String line) throws IOException {
        bw.write("---"+line);
        /**
         * apiVersion: v1
         * kind: Service
         * metadata:
         *   name: fuyi-home-svc
         * spec:
         *   type: NodePort
         *   ports:
         *     - port: 80
         *       nodePort: 30002
         *   selector:
         *     app: fuyi-home
         */
        bw.write("apiVersion: v1"+line);
        bw.write("kind: Service"+line);
        bw.write("metadata:"+line);
        bw.write("   name: "+serverName+line);
        String  dev=  System.getProperty("env");
        if(dev==null){
            bw.write("   namespace: dev"+line);
        }else{
            bw.write("   namespace: "+dev+line);
        }
        bw.write("spec:"+line);
        bw.write("   type: ClusterIP"+line);
        bw.write("   ports:"+line);
        bw.write("     - port: "+port+line);
        bw.write("       targetPort: "+port+line);
        bw.write("   selector:"+line);
        bw.write("     app: "+serverName+line);
    }

    /**
     * 创建deylopment信息
     * @param bw
     * @param line
     * @throws IOException
     */
    private void createDeploymentInfo(BufferedWriter bw, String line) throws IOException {
        /**
         * 编写yaml头  首先创建 deployment
         * apiVersion: v1
         * kind: deployment
         * metadata:
         *   name: fuyi-home-rc
         *   namespaces: test
         * spec:
         *   replicas: 2
         *   selector:
         *     app: fuyi-home
         *   template:
         *     metadata:
         *       labels:
         *         app: fuyi-home
         *     spec:
         *       containers:
         *       - name: fuyi-home-container
         *         image: fuyi-home:v1
         *         imagePullPolicy: IfNotPresent
         *         ports:
         *         - containerPort: 80
         */

        bw.write("---"+line);
        //创建版本号
        bw.write("apiVersion: apps/v1"+line);
        //创建的模式是Deployment
        bw.write("kind: Deployment"+line);
        bw.write("metadata:"+line);
        //创建deployment名字
        bw.write("  name: "+serverName+line);
        //设置命名空间
        String  dev=  System.getProperty("env");
        if(dev==null){
            bw.write("  namespace: dev"+line);
        }else{
            bw.write("  namespace: "+dev+line);
        }
        bw.write("spec:"+line);
        //设置创建几个pod
        bw.write("  replicas: 3"+line);
        bw.write("  selector:"+line);
        bw.write("    matchLabels:"+line);
        bw.write("      app: "+serverName+line);
        bw.write("  template:"+line);
        bw.write("    metadata:"+line);
        bw.write("      labels:"+line);
        bw.write("        app: "+serverName+line);
        bw.write("    spec:"+line);
        bw.write("      containers:"+line);
        bw.write("      - name: "+serverName+line);
        bw.write("        image: "+dockerUrl+line);
        bw.write("        imagePullPolicy: IfNotPresent"+line);
        bw.write("        ports:"+line);
        bw.write("        - containerPort: "+port+line);
    }

    private void createFilePath(File file) throws IOException {

        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
}
